package com.example.mvvmkotlin

import android.content.Context
import android.os.Environment
import android.util.Log
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.util.*

class Tool {

    fun createFanganPath(context: Context): String? {
        var prePath = ""
        prePath = getFileRoot(context)
        val filePath = prePath + File.separator + "fangmu" + File.separator + "file"
        val localFile = File(filePath)
        if (!localFile.exists()) {
            try {
                val re = localFile.mkdirs()
                Log.i(
                    "createFanganPath",
                    "createFanganPath: 不存在$filePath，创建$re"
                )
            } catch (e: Exception) {
                Log.i(
                    "createFanganPath",
                    "createFanganPath: " + e.message.toString()
                )
            }
        } else {
            Log.i("createFanganPath", "createFanganPath:存在 $filePath")
        }
        return filePath
    }

    //</editor-fold>
    //===================使用Properties方式，以文本文件读写配置，比SharedPreferences更灵活====================
    fun writeConfig(FilePath: String, NodeName: String, NodeValue: String?, FileName: String) {
        try {
            val configFileName = "$FilePath/fangmu/$FileName"
            val file = File(configFileName)
            if (!file.exists()) {
                try {
                    file.createNewFile()
                } catch (e: IOException) {
                    Log.i("TAG", "writeConfig: " + e.message.toString())
                    e.printStackTrace()
                }
            }
            val properties = Properties()
            val fs = FileInputStream(configFileName)
            properties.load(fs)
            properties.setProperty(NodeName, NodeValue)
            val fo = FileOutputStream(configFileName)
            properties.store(fo, "Update '$NodeName' value")
            fo.close()
            fs.close()
            //Log.i("TAG","写入Config "+NodeName+"="+NodeValue);
        } catch (e: java.lang.Exception) {
            Log.i("TAG", "写入Config错误: " + e.message.toString())
        }
    }

    fun readConfig(FilePath: String, NodeName: String?, FileName: String): String? {
        return try {
            val configFileName = "$FilePath/fangmu/$FileName"
            val file = File(configFileName)
            if (!file.exists()) {
                try {
                    file.createNewFile()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
            val properties = Properties()
            val fs = FileInputStream(configFileName)
            properties.load(fs)
            var nodeValue = properties.getProperty(NodeName)
            fs.close()
            if (nodeValue.isEmpty()) {
                nodeValue = "0"
            }
            nodeValue
        } catch (e: java.lang.Exception) {
            Log.i("TAG", "读取Config错误: " + e.message.toString())
            "0"
        }
    }


    //</editor-fold>
    fun getFileRoot(context: Context): String {
        //Environment.MEDIA_MOUNTED   sd卡正常挂载
        if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
            val external = context.getExternalFilesDir(null)
            if (external != null) {
                return external.absolutePath
            }
        }
        //  /data/user/0/应用包名/files
        return context.filesDir.absolutePath
    }
}